<div class="layuimini-main">

  <div class="layui-form layui-form-pane">

    <div class="layui-form-item">
      <label class="layui-form-label">秒杀商品名</label>
      <div class="layui-input-block">
        <input id="id" name="id" type="hidden">
        <input id="goodsName" class="layui-input" type="text" name="goodsName" placeholder="请选择需要参与秒杀活动的商品"
               autocomplete="off">
      </div>
    </div>

    <div class="layui-form-item">
      <label class="layui-form-label required">秒杀商品价格</label>
      <div class="layui-input-inline">
        <input id="seckillPrice" type="text" name="seckillPrice" lay-verify="required" lay-reqtext="商品价格不能为空"
               placeholder="请输入商品价格" value=""
               class="layui-input">
      </div>
    </div>

    <div class="layui-form-item">
      <label class="layui-form-label required">秒杀商品库存</label>
      <div class="layui-input-inline">
        <input id="stockCount" type="text" name="stockCount" lay-verify="required" lay-reqtext="商品库存不能为空"
               placeholder="请输入商品库存" value=""
               class="layui-input">
      </div>
    </div>

    <div class="layui-form-item">
      <label class="layui-form-label">秒杀活动开始时间</label>
      <div class="layui-input-block">
        <input type="text" id="starttime" name="startTime" class="layui-input" autocomplete="off"
               placeholder="请点击并选择秒杀活动开始时间">
      </div>
    </div>

    <div class="layui-form-item">
      <label class="layui-form-label">秒杀活动结束时间</label>
      <div class="layui-input-block">
        <input type="text" id="endtime" name="endTime" class="layui-input" autocomplete="off"
               placeholder="请点击并选择秒杀活动结束时间"></input>
      </div>
    </div>

    <div class="layui-form-item">
      <label class="layui-form-label required">商品图片</label>
      <div class="layui-input-block">
        <input id="goodsPic" type="text" readonly name="goodsPic" value="" class="layui-input">
        <img src="" id="goodsPhoto" style="width: 200px;border: none">
      </div>
    </div>

    <div class="layui-form-item">
      <div class="layui-input-block">
        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
      </div>
    </div>

  </div>
</div>

<script src="/js/jquery.min.js"></script>
<script src="/js/common.js"></script>
<script>
    function child(obj) {
        $('#id').val(obj.id);
        $('#goodsName').val(obj.goodsName);
        $('#seckillPrice').val(obj.seckillPrice);
        $('#stockCount').val(obj.stockCount);
        $('#goodsPic').val(obj.goodsPic);
        $('#goodsPhoto').attr('src', obj.goodsImg);

        layui.use('laydate', function () {
            var laydate = layui.laydate,
                $ = layui.jquery;

            /**
             * 初始化日期控件
             */
            laydate.render({
                elem: '#starttime',
                type: 'datetime', //选择时间
                format: 'yyyy-MM-dd HH:mm:ss',
                value: new Date(new Date(obj.startTime).getTime())
            });

            laydate.render({
                elem: '#endtime',
                type: 'datetime', //选择时间
                format: 'yyyy-MM-dd HH:mm:ss',
                value: new Date(new Date(obj.endTime).getTime())
            });
        })
    }

    layui.use(['table', 'form', 'tableSelect', 'laydate'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            laydate = layui.laydate,
            tableSelect = layui.tableSelect,
            $ = layui.jquery;

        /**
         * 初始化下拉框
         */
        tableSelect.render({
            elem: '#goodsName',
            checkedKey: 'id',
            table: {
                url: 'seckillGoods/seckillGoodsSelectData',
                cols: [
                    [
                        {type: 'radio'},
                        {field: 'id', title: 'ID'},
                        {field: 'goodsName', title: '商品名称'}
                    ]
                ]
            },
            done: function (elem, data) {
                $('#goodsId').val(data.data[0].id);
                elem.val(data.data[0].goodsName);
                $('#goodsPic').val(data.data[0].goodsPic);
                $('#goodsPhoto').attr('src', data.data[0].goodsImg);
            }
        })

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var json = JSON.stringify(data.field)
            var str = "商品名：" + data.field.goodsName +
                '<br>秒杀商品价格：' + data.field.seckillPrice +
                '<br>秒杀商品库存：' + data.field.stockCount +
                '<br>秒杀活动开始时间：' + data.field.startTime +
                '<br>秒杀活动结束时间：' + data.field.endTime +
                '<br>商品图片：' + data.field.goodsPic
            var index = layer.alert(str, {
                title: '最终的提交信息'
            }, function () {
                $.ajax({
                    url: "/seckillGoods/modifiedSeckillGoods",
                    type: "PUT",
                    contentType: 'application/json',
                    data: json,
                    success: function (data) {
                        if (data.code == 200) {
                            layer.msg(data.message)
                        } else {
                            layer.msg(data.message)
                        }
                    },
                    error: function () {
                        layer.msg(data.message)
                    }
                })

                parent.getSonValue(data.field)
                // 关闭弹出层
                layer.close(index);
                layer.close(parentIndex);
            });
        });
    });
</script>